import { upListRequest } from "../../server/index";
interface PayloadType {
  PageNo: number;
  PageSize: number;
  Keyword?: string;
}
// 排行榜页面数据
Page({
  data: {
    isDown: true, // 是否下拉
    loading: false,
    activeTab: 0, // 当前选项卡的索引
    searchValue: "", // 输入框的值
    headerList: ["排名", "UP主信息", "胡椒指数", "详情"],
    upList: [], // up主列表
    page: 1,
    isShowTop: false, // 滚动的距离
  },
  onReady() {
    this.refreshTop();
  },
  // 回顶部
  goTop() {
    wx.createSelectorQuery()
      .select("#scrollView")
      .node(({ node }) => {
        node.scrollTo({
          top: 0,
          duration: 300,
          animated: true,
        });
      })
      .exec();
  },
  // 滚动监听
  scrollHandler(e: any) {
    const { scrollTop } = e.detail;
    this.setData({
      isShowTop: scrollTop > 300,
    });
  },
  // 下拉刷新
  refreshTop() {
    this.setData({ page: 1, upList: [] });
    this.getUpList();
  },
  // 滚动到底部
  scrollBottom() {
    this.getUpList();
  },
  // 获取up主列表
  getUpList() {
    const { searchValue } = this.data;
    const payload: PayloadType = {
      PageNo: this.data.page++,
      PageSize: 20,
    };
    this.setData({ loading: true });
    searchValue && (payload.Keyword = searchValue);
    upListRequest(payload)
      .then((res: any) => {
        const { upList } = this.data;
        this.setData({
          upList: upList.concat(res.rows),
          isDown: false,
          loading: false,
        });
      })
      .catch(() => {
        this.setData({ loading: false });
      });
  },
  // 搜索回调
  onSearch(e: any) {
    this.setData({
      searchValue: e.detail,
      page: 1,
      upList: [],
    });
    this.getUpList();
  },
  // 选项卡切换
  onTabChange({ detail }: any) {
    const index = detail.index;
    const { headerList } = this.data;
    headerList.splice(2, 1, index === 1 ? "粉丝增量" : "胡椒指数");
    this.setData({
      headerList,
      activeTab: index,
    });
  },
  // 跳转到UP主详情
  jumpUpInfo(e: any) {
    const { mid } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/package/rank-up-info/index?mid=${mid}`,
    });
  },
});
